package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper,AddressBook> implements AddressBookService {

    public List<AddressBook> list(AddressBook addressBook) {
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<AddressBook>()
                .eq(addressBook.getUserId() != null, AddressBook::getUserId, addressBook.getUserId())
                .eq(addressBook.getPhone() != null, AddressBook::getPhone, addressBook.getPhone())
                .eq(addressBook.getIsDefault() != null, AddressBook::getIsDefault, addressBook.getIsDefault());
        return list(queryWrapper);
    }

    public void insert(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(0);
        save(addressBook);
    }

    public AddressBook getByAddressBookId(Long id) {
        return getById(id);
    }

    public void update(AddressBook addressBook) {
/*        String consignee = addressBook.getConsignee();
        String sex = addressBook.getSex();
        String phone = addressBook.getPhone();
        String detail = addressBook.getDetail();
        String label = addressBook.getLabel();
        Integer isDefault = addressBook.getIsDefault();
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<AddressBook>()
                .eq(AddressBook::getId, addressBook.getId())
                .set(consignee!=null,AddressBook::getConsignee, consignee)
                .set(sex!=null,AddressBook::getSex, sex)
                .set(phone!=null,AddressBook::getPhone, phone)
                .set(detail!=null,AddressBook::getDetail, detail)
                .set(label!=null,AddressBook::getLabel, label)
                .set(isDefault!=null,AddressBook::getIsDefault, isDefault);*/
        updateById(addressBook);
    }

    @Transactional
    public void setDefault(AddressBook addressBook) {
        //将当前用户的默认地址修改为非默认地址
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<AddressBook>()
                .eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .eq(AddressBook::getIsDefault,1)
                .set(AddressBook::getIsDefault, 0);
        update(updateWrapper);
        //将当前地址改为默认地址
        LambdaUpdateWrapper<AddressBook> updateWrapper1 = new LambdaUpdateWrapper<AddressBook>()
                .eq(AddressBook::getId, addressBook.getId())
                .set(AddressBook::getIsDefault, 1);
        update(updateWrapper1);
    }

    public void deleteById(Long id) {
        removeById(id);
    }

}
